package com.wangzhixuan.commons.utils;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageHelper;

/** 
* @ClassName: PageHelperUtils 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author thomas_liu  
* @date 2018年7月19日 下午7:27:51 
*  
*/
public class PageHelperUtils {
	static Logger logger = LoggerFactory.getLogger(PageHelperUtils.class);

	public static void startPage(HttpServletRequest request) {
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String orders = request.getParameter("orders");
		if (StringUtils.isBlank(pageNum)) {
			pageNum = "1";
		}
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "20";
		}
		logger.info("pageNum:{},pageSize:{}", pageNum, pageSize);
		PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		if (StringUtils.isNotBlank(orders)) {
			PageHelper.orderBy(camelToUnderline(orders));
		}
	}

	private static String camelToUnderline(String param) {
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append('_');
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

}
